//
//  SecurityController.m
//  YSSmartHome
//
//  Created by yanghao on 2016/9/23.
//  Copyright © 2016年 yahaong. All rights reserved.
//

#import "SecurityViewController.h"
#import "UIBarButtonItem+Extension.h"
#import "MeViewController.h"
#import "OpenApiService.h"
#import "QRCoderViewController.h"
#import "AddNewCameraController.h"
#import "DataManager.h"
#import "CameraViewCell.h"
#import "LiveVideoViewController.h"
#import "RecordViewController.h"
#import "MessageViewController.h"
#import "DeviceOperationViewController.h"
#import "CameraDeviceInfo.h"
#import "ReigsterCameraAccountController.h"
#import "NoCameralView.h"
#import "ZTLCCamera.h"
#import "ZTLCCameraUtil.h"
#import "GlobalConst.h"
#import "XAIGateway.h"
#import "UIView+HUD.h"

@interface SecurityViewController ()
<
UITableViewDelegate,
UITableViewDataSource,
CameraViewCellDelegate,
DataManagerRefreshDelegate
>
@property (nullable, nonatomic, copy)     NSString             *m_strAppId;
@property (nullable, nonatomic, copy)     NSString             *m_strAppSecret;
@property (nullable, nonatomic, copy)     NSString             *m_strSrv;
@property (nonatomic, assign)             NSInteger            m_iPort;
@property (nullable, nonatomic, copy)     NSString             *m_strUserTok;
@property (nullable, nonatomic, copy)     NSString             *serverInfo;
@property (nullable, nonatomic, strong)   UITextField          *m_textPhone;
@property (nullable, nonatomic, strong)   UITableView          *tableView;
@property (nullable, nonatomic, strong)   NoCameralView      *m_viewNonDev;
@property (nullable, nonatomic, strong)   NSMutableArray<ZTLCCamera *>  *m_cameras;
@property (nullable, nonatomic, strong)   DataManager        *dataManager;
@property (nullable,nonatomic, strong)    UIButton             *m_btnProtect;
@property (nullable, nonatomic, strong) UINavigationBar* navigationBar;
@property (nullable, nonatomic, strong) UINavigationItem *titleItem;
@end

@implementation SecurityViewController


#pragma mark - life cycle

- (void)dealloc
{
    debugLogInfo(@"SecurityViewController dealloc...");
    [kDataManager removeRefreshDelegate:self];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initSubviews];
    [self.dataManager addRefreshDelegate:self];
    [self dataRefresh:self.dataManager];
    self.m_btnProtect.selected = kMQTTSessionManager.currentGateway.pushActive;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(protectChangeHandle:) name:@"kZTProtectChangeNotifKey" object:nil];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [self.m_cameras makeObjectsPerformSelector:@selector(loadcameraDeviceInfo)];
}

#pragma mark - event response
- (void)protectChangeHandle:(NSNotification*)notification
{
     self.m_btnProtect.selected = kMQTTSessionManager.currentGateway.pushActive;
}
- (void)enterMeController
{
    MeViewController *meVc = [[MeViewController alloc]init];
    [self.navigationController pushViewController:meVc animated:YES];
}
- (void)addNewCamera
{
    AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    switch (status)
    {
        case AVAuthorizationStatusNotDetermined:
        {
            [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
                if (granted)
                {
                    __weak typeof(self)weakSelf = self;
                    dispatch_async(dispatch_get_main_queue(), ^{
                        __strong typeof(weakSelf)strongSelf = weakSelf;
                        if (!strongSelf) {
                            return ;
                        }
                        [strongSelf didTappedAddCameraButton];
                    });
                    
                }
                else
                {
                    debugLogInfo(@"用户拒绝");
                }
            }];
        }
            break;
        case AVAuthorizationStatusAuthorized:
        {
            // 已经开启授权，可继续
            [self didTappedAddCameraButton];
        }
            break;
        case AVAuthorizationStatusDenied:
        {
            UIAlertView* alerView = [[UIAlertView alloc]initWithTitle:@"相机权限未开启" message:@"相机权限未开启，请进入系统【设置】>【隐私】>【相机】中打开开关,开启相机功能" delegate:self cancelButtonTitle:@"好" otherButtonTitles:@"设置", nil];
            [alerView show];
        }
            break;
        case AVAuthorizationStatusRestricted:
        {
            [self.view yh_showMsgHUD:@"相机设备无法访问"];
        }
            break;
        default:
            break;
    }

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex NS_DEPRECATED_IOS(2_0, 9_0)
{
    if (buttonIndex == 1)
    {
        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
    }
}
- (void)didTappedAddCameraButton
{
    ZTLCCamera *m_camera = nil;
    for (ZTLCCamera *camera in self.m_cameras)
    {
        if (camera.strPhoneNum.length != 0)
        {
            m_camera = camera;
            break;
        }
    }
    if (m_camera != nil && m_camera.cameraDeviceInfo != nil)
    {
        QRCoderViewController *QRCoderVc = [QRCoderViewController  createQRCoderViewController];
        QRCoderVc.enterVcType = EnterScanControllerTypeAddNewCamera;
        [QRCoderVc setInfo:m_camera.cameraDeviceInfo.m_hc token:m_camera.cameraDeviceInfo.token phone:m_camera.strPhoneNum devView:self];
        [self.navigationController pushViewController:QRCoderVc animated:YES];
        return;
    }
    ZTUser *user =  kMQTTSessionManager.currentUser;
    [ZTLCCameraUtil fetchToken:user.m_strTel CompletionBlock:^(id returnValue) {
        LCOpenSDK_Api *m_hc  =  [ZTLCCameraUtil loadhc:returnValue];
        dispatch_async(dispatch_get_main_queue(), ^{
                QRCoderViewController *QRCoderVc = [QRCoderViewController  createQRCoderViewController];
                QRCoderVc.enterVcType = EnterScanControllerTypeAddNewCamera;
                [QRCoderVc setInfo:m_hc token:returnValue phone:user.m_strTel devView:self];
                [self.navigationController pushViewController:QRCoderVc animated:YES];
        });
    } WithFailureBlock:^(NSString *errMsg) {
        if (![errMsg isEqualToString:@"TK1004"]) {
            dispatch_async(dispatch_get_main_queue(), ^{
                ReigsterCameraAccountController *registerCameraVc = [[ReigsterCameraAccountController alloc]init];
                [self.navigationController pushViewController:registerCameraVc animated:YES];
            });
        }
    }];
}

- (void)protectButtonClick:(UIButton*)button
{
    if (kMQTTSessionManager.registerID.length == 0)
    {
        [self.view yh_showMsgHUD:@"通知还在初始化，请稍后再试"];
        return;
    }
    if ([self checkShockIsOpen])
    {
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    }

//    button.selected = !button.selected;
    [self setpush:!button.selected];
}
- (BOOL) checkShockIsOpen
{
    return [[NSUserDefaults standardUserDefaults] boolForKey:kZTShockSwitchIsOpen];
}

#pragma mark - DataManagerRefreshDelegate

- (void)dataRefresh:(DataManager *)manager
{
    NSMutableArray* cameras = [NSMutableArray array];
    [kDataManager devlistLock];
    for (ZTDevice *dev in self.dataManager.deviceListenList) {
        if (dev.devType == ZTDeviceTypeLCCamera ) {
            [cameras addObject:(ZTLCCamera *)dev];
        }
    }
    self.m_cameras = cameras;
    [kDataManager devlistUnlock];
    dispatch_async(dispatch_get_main_queue(), ^{
        self.m_viewNonDev.hidden = (self.m_cameras.count > 0);
        [self.tableView reloadData];
    });
}
#pragma mark - UITableViewDataSource
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.m_cameras.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CameraViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.delegate = self;
    cell.camera = self.m_cameras[indexPath.row];
    return cell;
}

#pragma mark - CameraViewCellDelegate

- (void)cameraViewLCCamera:(ZTLCCamera *)camera clickType:(CameraButtonType)type
{
    if (type == CameraButtonTypeUnKonw)
    {
        return;
    }
    CameraDeviceInfo *carmeraDeviceInfo = camera.cameraDeviceInfo;
    if (carmeraDeviceInfo == nil)
    {
        [self.view yh_showMsgHUD:@"设备正在初始化，请稍后再试"];
        [self.m_cameras makeObjectsPerformSelector:@selector(loadcameraDeviceInfo)];
        return;
    }
    NSString *m_strDevSelected = (carmeraDeviceInfo.devInfo)->ID;
    NSString *m_devAbilitySelected = (carmeraDeviceInfo.devInfo)->ability;
    NSInteger m_devChnSelected = (carmeraDeviceInfo.devInfo)->channelId[carmeraDeviceInfo.index];
    NSString *m_accessToken =[carmeraDeviceInfo token];
    if (type == CameraButtonTypeLive)
    {
        UIStoryboard *currentBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        LiveVideoViewController *liveVideoView = [currentBoard instantiateViewControllerWithIdentifier:@"LiveVideo"];
        liveVideoView.navigationItem.title  = @"实时视频";
        [self.navigationController pushViewController:liveVideoView animated:NO];

        [liveVideoView setInfo:m_accessToken Dev:m_strDevSelected Chn:m_devChnSelected PicUrl:carmeraDeviceInfo.picUrl Abl:m_devAbilitySelected];
    }
    else if (type == CameraButtonTypeVideo)
    {
        UIStoryboard *currentBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        RecordViewController *recordView = [currentBoard instantiateViewControllerWithIdentifier:@"Record"];
        [recordView setInfo:m_accessToken Dev:m_strDevSelected Chn:m_devChnSelected Type:DeviceRecord];
        [self.navigationController pushViewController:recordView animated:NO];
    }
    else if (type == CameraButtonTypeCloud)
    {
        UIStoryboard *currentBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        RecordViewController *recordView = [currentBoard instantiateViewControllerWithIdentifier:@"Record"];
        [recordView setInfo:m_accessToken Dev:m_strDevSelected Chn:m_devChnSelected Type:CloudRecord];
        [self.navigationController pushViewController:recordView animated:NO];
    }
    else if (type == CameraButtonTypeMessage)
    {
        UIStoryboard *currentBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        MessageViewController *msgView = [currentBoard instantiateViewControllerWithIdentifier:@"MessageView"];
        [self.navigationController pushViewController:msgView animated:NO];
        [msgView setInfo:carmeraDeviceInfo.m_hc token:m_accessToken dev:m_strDevSelected chn:m_devChnSelected];
    }
    else if (type == CameraButtonTypeSetting)
    {
        UIStoryboard *currentBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        DeviceOperationViewController *deviceOperationView  = [currentBoard instantiateViewControllerWithIdentifier:@"DeviceOperation"];
        [deviceOperationView setInfo:carmeraDeviceInfo.m_hc Token:m_accessToken Dev:m_strDevSelected Chn:m_devChnSelected];
        [self.navigationController pushViewController:deviceOperationView animated:NO];
    }
}

#pragma mark - private method
- (void)initSubviews
{
    self.view.backgroundColor = kBgColor;
    [self.view addSubview:self.navigationBar];
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.m_viewNonDev];
    [self.view addSubview:self.m_btnProtect];
    [self layoutPageSubviews];
    self.m_btnProtect.selected = kMQTTSessionManager.isProtection;
}
- (void)layoutPageSubviews
{
    [self.navigationBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.equalTo(self.view);
        make.height.equalTo(64);
    }];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.equalTo(self.view);
        make.top.equalTo(self.navigationBar.bottom);
    }];
    [self.m_viewNonDev mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.tableView);
    }];
    [self.m_btnProtect mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(-89);
        make.trailing.equalTo(self.view).offset(kIphoneSize_Widith(-15));
    }];
}
- (void)setpush:(BOOL) isPush
{
    uint8_t on = 0x01;
    uint8_t off = 0x00l;
    NSMutableArray* pushRules = [NSMutableArray array];
    //一路
    XAIJPushRule* jpushRuleOneWay0SwitchOn = [XAIJPushRule pushRuleWithForce:false valIsLuid:false luid:ZTDeviceTypeOneWay0Switch statusID:0x01 msg:@"%N被%T打开" statusbyte:&on statusLen:sizeof(on)];
    [pushRules addObject:jpushRuleOneWay0SwitchOn];
    XAIJPushRule* jpushRuleOneWay0SwitchOff = [XAIJPushRule pushRuleWithForce:false valIsLuid:false luid:ZTDeviceTypeOneWay0Switch statusID:0x01 msg:@"%N被%T关闭" statusbyte:&off statusLen:sizeof(off)];
    [pushRules addObject:jpushRuleOneWay0SwitchOff];
    
    XAIJPushRule* jpushRuleOneWaySwitchOn = [XAIJPushRule pushRuleWithForce:false valIsLuid:false luid:ZTDeviceTypeOneWaySwitch statusID:0x01 msg:@"%N被%T打开" statusbyte:&on statusLen:sizeof(on)];
    [pushRules addObject:jpushRuleOneWaySwitchOn];
    XAIJPushRule* jpushRuleOneWaySwitchOff = [XAIJPushRule pushRuleWithForce:false valIsLuid:false luid:ZTDeviceTypeOneWaySwitch statusID:0x01 msg:@"%N被%T关闭" statusbyte:&off statusLen:sizeof(off)];
    [pushRules addObject:jpushRuleOneWaySwitchOff];
    //二路
    XAIJPushRule* jpushRuleTwoWay0Switch1On = [XAIJPushRule pushRuleWithForce:false valIsLuid:false luid:ZTDeviceTypeTwoWay0Switch statusID:0x01 msg:@"%N%I被%T打开" statusbyte:&on statusLen:sizeof(on)];
    [pushRules addObject:jpushRuleTwoWay0Switch1On];
    XAIJPushRule* jpushRuleTwoWay0Switch1Off = [XAIJPushRule pushRuleWithForce:false valIsLuid:false luid:ZTDeviceTypeTwoWay0Switch statusID:0x01 msg:@"%N%I被%T关闭" statusbyte:&off statusLen:sizeof(off)];
    [pushRules addObject:jpushRuleTwoWay0Switch1Off];
    XAIJPushRule* jpushRuleTwoWay0Switch2On = [XAIJPushRule pushRuleWithForce:false valIsLuid:false luid:ZTDeviceTypeTwoWay0Switch statusID:0x02 msg:@"%N%I被%T打开" statusbyte:&on statusLen:sizeof(on)];
    [pushRules addObject:jpushRuleTwoWay0Switch2On];
    XAIJPushRule* jpushRuleTwoWay0Switch2Off = [XAIJPushRule pushRuleWithForce:false valIsLuid:false luid:ZTDeviceTypeTwoWay0Switch statusID:0x02 msg:@"%N%I被%T关闭" statusbyte:&off statusLen:sizeof(off)];
    [pushRules addObject:jpushRuleTwoWay0Switch2Off];
    XAIJPushRule* jpushRuleTwoWaySwitch1On = [XAIJPushRule pushRuleWithForce:false valIsLuid:false luid:ZTDeviceTypeTwoWaySwitch statusID:0x01 msg:@"%N%I被%T打开" statusbyte:&on statusLen:sizeof(on)];
    [pushRules addObject:jpushRuleTwoWaySwitch1On];
    XAIJPushRule* jpushRuleTwoWaySwitch1Off = [XAIJPushRule pushRuleWithForce:false valIsLuid:false luid:ZTDeviceTypeTwoWaySwitch statusID:0x01 msg:@"%N%I被%T关闭" statusbyte:&off statusLen:sizeof(off)];
    [pushRules addObject:jpushRuleTwoWaySwitch1Off];
    XAIJPushRule* jpushRuleTwoWaySwitch2On = [XAIJPushRule pushRuleWithForce:false valIsLuid:false luid:ZTDeviceTypeTwoWaySwitch statusID:0x02 msg:@"%N%I被%T打开" statusbyte:&on statusLen:sizeof(on)];
    [pushRules addObject:jpushRuleTwoWaySwitch2On];
    XAIJPushRule* jpushRuleTwoWaySwitch2Off = [XAIJPushRule pushRuleWithForce:false valIsLuid:false luid:ZTDeviceTypeTwoWaySwitch statusID:0x02 msg:@"%N%I被%T关闭" statusbyte:&off statusLen:sizeof(off)];
    [pushRules addObject:jpushRuleTwoWaySwitch2Off];
    //三路
    XAIJPushRule* jpushRuleTheeWay0Switch1On = [XAIJPushRule pushRuleWithForce:false valIsLuid:false luid:ZTDeviceTypeThreeWay0Switch statusID:0x01 msg:@"%N%I被%T打开" statusbyte:&on statusLen:sizeof(on)];
    [pushRules addObject:jpushRuleTheeWay0Switch1On];
    
    XAIJPushRule* jpushRuleTheeWay0Switch1Off = [XAIJPushRule pushRuleWithForce:false valIsLuid:false luid:ZTDeviceTypeThreeWay0Switch statusID:0x01 msg:@"%N%I被%T关闭" statusbyte:&off statusLen:sizeof(off)];
    [pushRules addObject:jpushRuleTheeWay0Switch1Off];
    XAIJPushRule* jpushRuleTheeWay0Switch2On = [XAIJPushRule pushRuleWithForce:false valIsLuid:false luid:ZTDeviceTypeThreeWay0Switch statusID:0x02 msg:@"%N%I被%T打开" statusbyte:&on statusLen:sizeof(on)];
    [pushRules addObject:jpushRuleTheeWay0Switch2On];
    XAIJPushRule* jpushRuleTheeWay0Switch2Off = [XAIJPushRule pushRuleWithForce:false valIsLuid:false luid:ZTDeviceTypeThreeWay0Switch statusID:0x02 msg:@"%N%I被%T关闭" statusbyte:&off statusLen:sizeof(off)];
    [pushRules addObject:jpushRuleTheeWay0Switch2Off];
    XAIJPushRule* jpushRuleTheeWay0Switch3On = [XAIJPushRule pushRuleWithForce:false valIsLuid:false luid:ZTDeviceTypeThreeWay0Switch statusID:0x03 msg:@"%N%I被%T打开" statusbyte:&on statusLen:sizeof(on)];
    [pushRules addObject:jpushRuleTheeWay0Switch3On];
    XAIJPushRule* jpushRuleTheeWay0Switch3Off = [XAIJPushRule pushRuleWithForce:false valIsLuid:false luid:ZTDeviceTypeThreeWay0Switch statusID:0x03 msg:@"%N%I被%T关闭" statusbyte:&off statusLen:sizeof(off)];
    [pushRules addObject:jpushRuleTheeWay0Switch3Off];
    XAIJPushRule* jpushRuleTheeWaySwitch1On = [XAIJPushRule pushRuleWithForce:false valIsLuid:false luid:ZTDeviceTypeThreeWaySwitch statusID:0x01 msg:@"%N%I被%T打开" statusbyte:&on statusLen:sizeof(on)];
    [pushRules addObject:jpushRuleTheeWaySwitch1On];
    XAIJPushRule* jpushRuleTheeWaySwitch1Off = [XAIJPushRule pushRuleWithForce:false valIsLuid:false luid:ZTDeviceTypeThreeWaySwitch statusID:0x01 msg:@"%N%I被%T关闭" statusbyte:&off statusLen:sizeof(off)];
    [pushRules addObject:jpushRuleTheeWaySwitch1Off];
    XAIJPushRule* jpushRuleTheeWaySwitch2On = [XAIJPushRule pushRuleWithForce:false valIsLuid:false luid:ZTDeviceTypeThreeWaySwitch statusID:0x02 msg:@"%N%I被%T打开" statusbyte:&on statusLen:sizeof(on)];
    [pushRules addObject:jpushRuleTheeWaySwitch2On];
    XAIJPushRule* jpushRuleTheeWaySwitch2Off = [XAIJPushRule pushRuleWithForce:false valIsLuid:false luid:ZTDeviceTypeThreeWaySwitch statusID:0x02 msg:@"%N%I被%T关闭" statusbyte:&off statusLen:sizeof(off)];
    [pushRules addObject:jpushRuleTheeWaySwitch2Off];
    XAIJPushRule* jpushRuleTheeWaySwitch3On = [XAIJPushRule pushRuleWithForce:false valIsLuid:false luid:ZTDeviceTypeThreeWaySwitch statusID:0x03 msg:@"%N%I被%T打开" statusbyte:&on statusLen:sizeof(on)];
    [pushRules addObject:jpushRuleTheeWaySwitch3On];
    XAIJPushRule* jpushRuleTheeWaySwitch3Off = [XAIJPushRule pushRuleWithForce:false valIsLuid:false luid:ZTDeviceTypeThreeWaySwitch statusID:0x03 msg:@"%N%I被%T关闭" statusbyte:&off statusLen:sizeof(off)];
    [pushRules addObject:jpushRuleTheeWaySwitch3Off];
    //四路
    XAIJPushRule* jpushRuleFourWay0Switch1On = [XAIJPushRule pushRuleWithForce:false valIsLuid:false luid:ZTDeviceTypeFourWay0Switch statusID:0x01 msg:@"%N%I被%T打开" statusbyte:&on statusLen:sizeof(on)];
    [pushRules addObject:jpushRuleFourWay0Switch1On];
    XAIJPushRule* jpushRuleFoureWay0Switch1Off = [XAIJPushRule pushRuleWithForce:false valIsLuid:false luid:ZTDeviceTypeFourWay0Switch statusID:0x01 msg:@"%N%I被%T关闭" statusbyte:&off statusLen:sizeof(off)];
    [pushRules addObject:jpushRuleFoureWay0Switch1Off];
    XAIJPushRule* jpushRuleFourWay0Switch2On = [XAIJPushRule pushRuleWithForce:false valIsLuid:false luid:ZTDeviceTypeFourWay0Switch statusID:0x02 msg:@"%N%I被%T打开" statusbyte:&on statusLen:sizeof(on)];
    [pushRules addObject:jpushRuleFourWay0Switch2On];
    XAIJPushRule* jpushRuleFourWay0Switch2Off = [XAIJPushRule pushRuleWithForce:false valIsLuid:false luid:ZTDeviceTypeFourWay0Switch statusID:0x02 msg:@"%N%I被%T关闭" statusbyte:&off statusLen:sizeof(off)];
    [pushRules addObject:jpushRuleFourWay0Switch2Off];
    XAIJPushRule* jpushRuleFourWay0Switch3On = [XAIJPushRule pushRuleWithForce:false valIsLuid:false luid:ZTDeviceTypeFourWay0Switch statusID:0x03 msg:@"%N%I被%T打开" statusbyte:&on statusLen:sizeof(on)];
    [pushRules addObject:jpushRuleFourWay0Switch3On];
    XAIJPushRule* jpushRuleFourWay0Switch3Off = [XAIJPushRule pushRuleWithForce:false valIsLuid:false luid:ZTDeviceTypeFourWay0Switch statusID:0x03 msg:@"%N%I被%T关闭" statusbyte:&off statusLen:sizeof(off)];
    [pushRules addObject:jpushRuleFourWay0Switch3Off];
    XAIJPushRule* jpushRuleFourWay0Switch4On = [XAIJPushRule pushRuleWithForce:false valIsLuid:false luid:ZTDeviceTypeFourWay0Switch statusID:0x04 msg:@"%N%I被%T打开" statusbyte:&on statusLen:sizeof(on)];
    [pushRules addObject:jpushRuleFourWay0Switch4On];
    XAIJPushRule* jpushRuleFourWay0Switch4Off = [XAIJPushRule pushRuleWithForce:false valIsLuid:false luid:ZTDeviceTypeFourWay0Switch statusID:0x04 msg:@"%N%I被%T关闭" statusbyte:&off statusLen:sizeof(off)];
    [pushRules addObject:jpushRuleFourWay0Switch4Off];
    XAIJPushRule* jpushRuleFourWaySwitch1On = [XAIJPushRule pushRuleWithForce:false valIsLuid:false luid:ZTDeviceTypeFourWaySwitch statusID:0x01 msg:@"%N%I被%T打开" statusbyte:&on statusLen:sizeof(on)];
    [pushRules addObject:jpushRuleFourWaySwitch1On];
    XAIJPushRule* jpushRuleFoureWaySwitch1Off = [XAIJPushRule pushRuleWithForce:false valIsLuid:false luid:ZTDeviceTypeFourWaySwitch statusID:0x01 msg:@"%N%I被%T关闭" statusbyte:&off statusLen:sizeof(off)];
    [pushRules addObject:jpushRuleFoureWaySwitch1Off];
    XAIJPushRule* jpushRuleFourWaySwitch2On = [XAIJPushRule pushRuleWithForce:false valIsLuid:false luid:ZTDeviceTypeFourWaySwitch statusID:0x02 msg:@"%N%I被%T打开" statusbyte:&on statusLen:sizeof(on)];
    [pushRules addObject:jpushRuleFourWaySwitch2On];
    XAIJPushRule* jpushRuleFourWaySwitch2Off = [XAIJPushRule pushRuleWithForce:false valIsLuid:false luid:ZTDeviceTypeFourWaySwitch statusID:0x02 msg:@"%N%I被%T关闭" statusbyte:&off statusLen:sizeof(off)];
    [pushRules addObject:jpushRuleFourWaySwitch2Off];
    XAIJPushRule* jpushRuleFourWaySwitch3On = [XAIJPushRule pushRuleWithForce:false valIsLuid:false luid:ZTDeviceTypeFourWaySwitch statusID:0x03 msg:@"%N%I被%T打开" statusbyte:&on statusLen:sizeof(on)];
    [pushRules addObject:jpushRuleFourWaySwitch3On];
    XAIJPushRule* jpushRuleFourWaySwitch3Off = [XAIJPushRule pushRuleWithForce:false valIsLuid:false luid:ZTDeviceTypeFourWaySwitch statusID:0x03 msg:@"%N%I被%T关闭" statusbyte:&off statusLen:sizeof(off)];
    [pushRules addObject:jpushRuleFourWaySwitch3Off];
    XAIJPushRule* jpushRuleFourWaySwitch4On = [XAIJPushRule pushRuleWithForce:false valIsLuid:false luid:ZTDeviceTypeFourWaySwitch statusID:0x04 msg:@"%N%I被%T打开" statusbyte:&on statusLen:sizeof(on)];
    [pushRules addObject:jpushRuleFourWaySwitch4On];
    XAIJPushRule* jpushRuleFourWaySwitch4Off = [XAIJPushRule pushRuleWithForce:false valIsLuid:false luid:ZTDeviceTypeFourWaySwitch statusID:0x04 msg:@"%N%I被%T关闭" statusbyte:&off statusLen:sizeof(off)];
    [pushRules addObject:jpushRuleFourWaySwitch4Off];
    
    //一路开关
    XAIJPushRule* jpushRuleOneWaySocketOn = [XAIJPushRule pushRuleWithForce:false valIsLuid:false luid:ZTDeviceTypeOneWaySocket statusID:0x01 msg:@"%N被%T打开" statusbyte:&on statusLen:sizeof(on)];
    [pushRules addObject:jpushRuleOneWaySocketOn];
    XAIJPushRule* jpushRuleOneWaySocketOff = [XAIJPushRule pushRuleWithForce:false valIsLuid:false luid:ZTDeviceTypeOneWaySocket statusID:0x01 msg:@"%N被%T关闭" statusbyte:&off statusLen:sizeof(off)];
    [pushRules addObject:jpushRuleOneWaySocketOff];
    //烟雾
    XAIJPushRule* jpushRuleSmokeOn = [XAIJPushRule pushRuleWithForce:true valIsLuid:false luid:ZTDeviceTypeSmoke statusID:0x01 msg:@"%N:触发火灾烟雾报警!" statusbyte:&on statusLen:sizeof(on)];
    [pushRules addObject:jpushRuleSmokeOn];
    //燃气
    XAIJPushRule* jpushRuleGasOn = [XAIJPushRule pushRuleWithForce:true valIsLuid:false luid:ZTDeviceTypeGAS statusID:0x01 msg:@"%N:触发燃气泄露报警!" statusbyte:&on statusLen:sizeof(on)];
    [pushRules addObject:jpushRuleGasOn];
    //红外
    XAIJPushRule* jpushInfraredOn = [XAIJPushRule pushRuleWithForce:false valIsLuid:false luid:ZTDeviceTypeInfraredDetector statusID:0x01 msg:@"%N:有异常人员闯入!" statusbyte:&on statusLen:sizeof(on)];
    [pushRules addObject:jpushInfraredOn];
    //一键报警
    XAIJPushRule* jpushRuleAlarmkeyOn = [XAIJPushRule pushRuleWithForce:true valIsLuid:false luid:ZTDeviceTypeAlarmkey statusID:0x01 msg:@"%N:%G所在家庭请求紧急援助!" statusbyte:&on statusLen:sizeof(on)];
    [pushRules addObject:jpushRuleAlarmkeyOn];
    XAIJPushRule* jpushRuleWaterlogOn = [XAIJPushRule pushRuleWithForce:true valIsLuid:false luid:ZTDeviceTypeFlooding statusID:0x01 msg:@"%N:触发漏水报警!" statusbyte:&on statusLen:sizeof(on)];
    [pushRules addObject:jpushRuleWaterlogOn];
    //门磁
    XAIJPushRule* jpushRuleDoorsensorOn = [XAIJPushRule pushRuleWithForce:false valIsLuid:false luid:ZTDeviceTypeZTDoorsensor statusID:0x01 msg:@"%N被打开" statusbyte:&on statusLen:sizeof(on)];
    [pushRules addObject:jpushRuleDoorsensorOn];
    XAIJPushRule* jpushRuleDoorsensorOff = [XAIJPushRule pushRuleWithForce:false valIsLuid:false luid:ZTDeviceTypeZTDoorsensor statusID:0x01 msg:@"%N被关闭" statusbyte:&off statusLen:sizeof(off)];
    [pushRules addObject:jpushRuleDoorsensorOff];
    
//    [kMQTTSessionManager setJPushRule:kMQTTSessionManager.currentUser.currentBindApsn registerID:kMQTTSessionManager.registerID enable:isPush configs:pushRules];
    
   NSError* error =  [kMQTTSessionManager.xai setJPushRule:kMQTTSessionManager.currentUser.currentBindApsn registerID:kMQTTSessionManager.registerID enable:isPush configs:pushRules];
    if (error.code == 0) {
        NSLog(@"撤布防成功");
    }
    else
    {
        NSLog(@"%@",error.localizedDescription);
    }
}


#pragma mark - getters and setters
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]init];
        _tableView.delegate = self;
        _tableView.dataSource =self;
        [_tableView registerClass:[CameraViewCell class] forCellReuseIdentifier:@"cell"];
        _tableView.backgroundColor = kBgColor;
        _tableView.sectionFooterHeight = 10;
        _tableView.rowHeight = Device_Cell_Height;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.tableFooterView = [[UIView alloc]init];
    }
    return _tableView;
}

- (NoCameralView *)m_viewNonDev
{
    if (!_m_viewNonDev) {
        _m_viewNonDev = [[NoCameralView alloc]init];
        _m_viewNonDev.title = @"     亲  你还未添加摄像头\n你可以点击“+”进行添加设置";
        _m_viewNonDev.icon = @"security_icon_nodevice";
    }
    return _m_viewNonDev;
}
- (UIButton *)m_btnProtect
{
    if (!_m_btnProtect) {
        _m_btnProtect = [UIButton buttonWithType:UIButtonTypeCustom];
        [_m_btnProtect setImage:[UIImage imageNamed:@"security_icon_protectclose"] forState:UIControlStateNormal];
         [_m_btnProtect setImage:[UIImage imageNamed:@"security_icon_protecton"] forState:UIControlStateSelected];
        [_m_btnProtect addTarget:self action:@selector(protectButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _m_btnProtect;
}
- (DataManager *)dataManager
{
    if (!_dataManager) {
        _dataManager = [DataManager sharedManager];
    }
    return _dataManager;
}

- (UINavigationBar *)navigationBar
{
    if (!_navigationBar) {
        _navigationBar = [[UINavigationBar alloc]init];
        [_navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationBarbackgroundImage"] forBarMetrics:UIBarMetricsDefault];
        NSMutableDictionary *barTextAttrs = [NSMutableDictionary dictionary];
        barTextAttrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
        barTextAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:17];
        [_navigationBar setTitleTextAttributes:barTextAttrs];
        _navigationBar.items = @[self.titleItem];
    }
    return _navigationBar;
}
- (UINavigationItem *)titleItem
{
    if (!_titleItem) {
        _titleItem = [[UINavigationItem alloc]init];
        _titleItem.leftBarButtonItem = [UIBarButtonItem createBarbuttonItem:@"home_icon_me" target:self action:@selector(enterMeController)];
        _titleItem.rightBarButtonItem =  [UIBarButtonItem createBarbuttonItem:@"security_add" target:self action:@selector(didTappedAddCameraButton)];
        _titleItem.title = @"智能安防";
    }
    return _titleItem;
}
@end
